package com.sprouting.ops.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsProductQuestion;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.bo.OpsProductQuestionBo;
import com.sprouting.ops.model.qo.OpsProductQuestionQo;
import com.sprouting.ops.service.DromaraFileStorageService;
import com.sprouting.ops.service.OpsProductQuestionService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品常用问题(OpsProductQuestion)(OPS_PRODUCT_QUESTION) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-08-17 11:28:00
 */
@Slf4j
@RestController
@RequestMapping("opsProductQuestion")
public class OpsProductQuestionController {

    @Resource
    private OpsProductQuestionService opsProductQuestionService;
    @Resource
    private DromaraFileStorageService dromaraFileStorageService;


    /**
     * 首页-常见问题
     * @return
     */
    @PostMapping("getHomeProductQuestion")
    public ResultJson getHomeProductQuestion(){
        return ResultJson.success(opsProductQuestionService.getHomeProductQuestion());
    }

    /**
     * 分页查询产品常用问题
     * @param query 查询参数
     * @return 返回查询结果
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(OpsProductQuestionQo query){
        if (query == null){
            query = new OpsProductQuestionQo();
        }
        Page page = query.getPage();
        return ResultJson.success(opsProductQuestionService.getListPage(page, query));
    }

    /**
     * 删除产品常用问题
     * @param id 产品常用问题id
     * @return
     */
    @PostMapping("delOpsProductQuestion")
    @IdempotentSingle
    public ResultJson delOpsProductQuestion(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        List<DromaraFileStorage> bindList = dromaraFileStorageService.getByBindId(id);

        opsProductQuestionService.delOpsProductQuestion(id, bindList);
        return ResultJson.success();
    }

    /**
     * 更新产品常用问题
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsProductQuestion")
    @IdempotentSingle
    public ResultJson updOpsProductQuestion(@Validated(UpdGroup.class) OpsProductQuestionBo bo){
        String msg = checkOpsProductQuestion(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        OpsProductQuestion question = BeanUtil.copyProperties(bo, OpsProductQuestion.class);
        EntityTool.updBase(question);

        // 提取Markdown的数据
        // 修改的时候也要提取，然后转换为生产的数据，这里要注意的是，修改的时候，不考虑去删除旧的数据的问题，到时候删除的时候，直接使用objId统一处理即可，因为这个数据量还是比较小的，没必要找的旧的去删，新的去加，搞麻烦了
        List<DromaraFileStorage> list = getMarkdownImage(question);

        opsProductQuestionService.updOpsProductQuestion(question, list);
        return ResultJson.success(bo);
    }

    /**
     * 保存产品常用问题
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsProductQuestion")
    @IdempotentSingle
    public ResultJson saveOpsProductQuestion(@Validated(AddGroup.class) OpsProductQuestionBo bo){
        String msg = checkOpsProductQuestion(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        OpsProductQuestion question = BeanUtil.copyProperties(bo, OpsProductQuestion.class);
        question.setId(IdTool.getId());
        if (question.getFrequency() == null){
            question.setFrequency(1);
        }
        if (question.getStatus() == null){
            question.setStatus(ConstantBase.ProductQuestionStatus.OPEN);
        }
        if (question.getFraction() == null){
            question.setFraction(50d);
        }
        EntityTool.saveBase(question);

        // 提取Markdown的数据
        List<DromaraFileStorage> list = getMarkdownImage(question);

        opsProductQuestionService.saveOpsProductQuestion(question, list);
        return ResultJson.success(bo);
    }

    /**
     * 获取图片信息
     * @param question 问题
     * @return 提取到的图片信息用于转正式
     */
    private List<DromaraFileStorage> getMarkdownImage(OpsProductQuestion question) {
        List<DromaraFileStorage> list = new ArrayList<>();
        List<DromaraFileStorage> detailedlList = dromaraFileStorageService.getMarkdownImagesUrlToFileStorage(question.getProblemDetailed(), question.getId(), ConstantBase.FileSourceType.PROBLEM_DETAILED);
        if (CollUtil.isNotEmpty(detailedlList)){
            list.addAll(detailedlList);
        }
        List<DromaraFileStorage> solutionToList = dromaraFileStorageService.getMarkdownImagesUrlToFileStorage(question.getSolutionTo(), question.getId(), ConstantBase.FileSourceType.SOLUTION_TO);
        if (CollUtil.isNotEmpty(solutionToList)){
            list.addAll(solutionToList);
        }

        return list;
    }

    /**
     * 产品常用问题数据校验
     * @param bo 产品常用问题 校验数据
     * @return 非null则有错误
     */
    private String checkOpsProductQuestion(OpsProductQuestionBo bo){

        return null;
    }

}
